import { Component, ChangeDetectionStrategy, OnInit, ChangeDetectorRef } from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd, ParamMap, Params } from '@angular/router';
import { Location } from '@angular/common';
import { filter, map } from 'rxjs/operators';
import * as R from 'ramda';
import { BuilderViewerComponent } from '../builder-viewer/builder-viewer.component';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MainComponent implements OnInit {

  isBuilder: boolean = false;

  builderViewer: BuilderViewerComponent;

  constructor(
    public router: Router,
    public activatedRoute: ActivatedRoute,
    public local: Location,
    public changeDetectorRef: ChangeDetectorRef,
  ) {}

  ngOnInit() {
    this.isBuilder = this.verifyPathIsBuilder();
    
    this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
    ).subscribe(() => {
      this.isBuilder = this.verifyPathIsBuilder();
      this.changeDetectorRef.markForCheck();
    })
  }

  verifyPathIsBuilder() {
    const url = this.local.path();

    if (R.test(/builder/g, url)) {
      return true;
    }
    
    return false;
  }

  navigateToBuilderForm() {
    this.router.navigate(['builder', 'form']);
  }

  back() {
    this.local.back();
  }

  saveAndRender() { 
  }

  render () {
  }
}
